import request from '@/utils/request'
import {ElMessage, ElMessageBox} from "element-plus";


// 定义56个名族
export const ethnics = ref([]);
// 定义园区
export const parks = ref([]);
// 定义工种
export const jobTypes = [
    { value: 1, label: '从事接触直接入口食品的人员' },
    { value: 2, label: '药品生产、经营企业和医疗机构直接接触药品的工作人员' },
    { value: 3, label: '直接从事化妆品生产的人员' },
    { value: 4, label: '公共场所直接为顾客提供服务的人员' },
    { value: 5, label: '直接从事供、管水的人员' },
    { value: 6, label: '消毒产品生产企业负责生产、分装或质量检验的人员' }
]
// 定义预约医院
export const reserveHospitals = ref([]);
// 定义医院的预约时间
export const reserveHours = ref([]);
// 定义所有的医院
export const hospitals = ref([]);
// 定义医院地址
export const reserveHospitalAddress = ref([]);

export const base64Prefix = "data:image/png;base64,"

// 查询56个基础名族数据
function listEthnic() {
    return request({
        url: '/employee/personApply/listEthnic',
        method: 'get',
    })
}

// 查询园区下拉框选项
function listParkId() {
    return request({
        url: '/employee/personApply/listParkId',
        method: 'get',
    })
}

// 查询工种下拉框选项
function listJobTypes() {
    return request({
        url: '/employee/personApply/listJobTypes',
        method: 'get',
    })
}

// 根据园区id查询医院下拉框选项
function listReserveHospital(parkId) {
    return request({
        url: '/employee/reserve/listHospital/' + parkId,
        method: 'get',
    })
}

// 查询所有医院
function listHospitals() {
    return request({
        url: '/employee/reserve/listHospitals',
        method: 'get',
    })
}

// 根据医院id查询预约时间下拉框选项
function listReserveHour(hospitalId) {
    return request({
        url: '/employee/reserve/listHour/' + hospitalId,
        method: 'get',
    })
}

// 查询所有医院地址
function listReserveHospitalAddress() {
    return request({
        url: '/employee/reserve/listAddress/',
        method: 'get',
    })
}

//加载56个名族下拉框选项
export function loadEthnic() {
    listEthnic().then(response => {
        ethnics.value = response;
    })
}

//加载园区下拉框选项
export function loadParkId() {
    listParkId().then(response => {
        parks.value = response;
    })
}

//加载工种下拉框选项
export function loadJobTypes() {
    listJobTypes().then(response => {
        jobTypes.value = response;
    })
}

// 加载预约医院下拉框选项
export function loadReserveHospital(parkId) {
    listReserveHospital(parkId).then(response => {
        if (response.data) {
            reserveHospitals.value = response.data;
        } else {
            ElMessage.error('未检测到申报园区所对应的医院！');
        }
    })
}

//  加载预约时间下拉框选项
export function loadReserveHour(hospitalId) {
    listReserveHour(hospitalId).then(response => {
        reserveHours.value = response.data;
    })
}

//  加载所有的医院
export function loadAllHospitals() {
    listHospitals().then(response => {
        hospitals.value = response.data;
    })
}

//  加载所有医院的地址
export function loadAllHospitalAddress() {
    listReserveHospitalAddress().then(response => {
        reserveHospitalAddress.value = response.data;
    })
}

// 定义 getCookie 函数
export function getCookie(name) {
    const cookies = document.cookie.split('; ');
    for (const cookie of cookies) {
        const [key, value] = cookie.split('=');
        if (key === name) {
            return decodeURIComponent(value);
        }
    }
    return null;
}

/** 表单规则校验 */
export const rules = reactive({
    name: [
        {required: true, message: '请输入姓名', trigger: 'blur'}
    ],
    idCard: [
        {required: true, message: '请输入身份证号', trigger: 'blur'},
        {pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '身份证号格式不正确'},
        {
            validator: (rule, value, callback) => {
                if (value) {
                    // 验证身份证格式
                    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                    if (!reg.test(value)) {
                        callback(new Error('身份证号格式不正确'));
                        return;
                    }

                    // 从身份证中提取出生日期
                    let birthYear, birthMonth, birthDay;
                    if (value.length === 18) {
                        // 18位身份证
                        birthYear = parseInt(value.substring(6, 10));
                        birthMonth = parseInt(value.substring(10, 12));
                        birthDay = parseInt(value.substring(12, 14));
                    } else if (value.length === 15) {
                        // 15位身份证
                        birthYear = parseInt('19' + value.substring(6, 8));
                        birthMonth = parseInt(value.substring(8, 10));
                        birthDay = parseInt(value.substring(10, 12));
                    }

                    // 计算年龄
                    const currentDate = new Date();
                    const currentYear = currentDate.getFullYear();
                    const currentMonth = currentDate.getMonth() + 1;
                    const currentDay = currentDate.getDate();

                    let age = currentYear - birthYear;
                    if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDay < birthDay)) {
                        age--;
                    }
                    // 验证年龄是否大于等于18岁
                    if (age < 18) {
                        callback(new Error('健康证18岁以下不能预约！'));
                    } else {
                        callback();
                    }
                } else {
                    callback();
                }
            },
            trigger: 'blur'
        }
    ],
    age: [
        {required: true, message: '请输入年龄', trigger: 'blur'},
        {type: 'number', message: '年龄必须为数字值'}
    ],
    gender: [
        {required: true, message: '请选择性别', trigger: 'change'}
    ],
    ethnic: [
        {required: true, message: '请选择民族', trigger: 'change'}
    ],
    parkId: [
        {required: true, message: '请选择所属区域', trigger: 'change'}
    ],
    jobTypeId: [
        {required: true, message: '请选择工种', trigger: 'change'}
    ],
    phone: [
        {required: true, message: '请输入联系电话', trigger: 'blur'},
        {pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确'}
    ],
    address: [
        {required: true, message: '请输入家庭地址', trigger: 'blur'}
    ],
    unitName: [
        {required: true, message: '请输入企业名称', trigger: 'blur'}
    ],
    socialCreditCode: [
        {required: true, message: '请输入机构代码', trigger: 'blur'}
    ],
    reserveHospital: [
        {required: true, message: '请选择预约医院', trigger: 'blur'}
    ],
    reserveDate: [
        {required: true, message: '请选择预约日期', trigger: 'blur'}
    ],
    reserveHour: [
        {required: true, message: '请选择预约时间', trigger: 'blur'}
    ],
    uploadFile: [
        {required: true, message: '文件不能为空', trigger: 'blur'}
    ],
})

// 通知方法
export const openNotice = () => {
    const title = '关于健康证办理的公告'
    const notice = '<p class="notice">尊敬的各位市民：</p>'
        + '<p class="notice">您好！感谢您一直以来对我们工作的支持与理解。近期，由于健康证新旧系统替换，给广大市民健康证办理工作带来了一定的影响。为确保大家能够顺利办理健康证，现将相关事宜公告如下：</p>'
        + '<p class="notice">一、自端午节假期前已申请但未能成功办理健康证的人员，请通过新系统进行预约。我们将根据预约情况，合理安排办理时间，确保大家能够尽快完成健康证的办理。</p>'
        + '<p class="notice">二、新系统登录地址：</p>'
        + '<p class="notice">1、个人申报页面链接</p>'
        + '<p style="color: #1c84c6">http://117.157.64.139:9000/employee/personApply</p>'
        + '<p class="notice">备注：无需登录，直接填写信息</p>'
        + '<p class="notice">2、企业申报页面</p>'
        + '<p style="color: #1c84c6">http://117.157.64.139:9000/employee/unitApply</p>'
        + '<p class="notice">备注：无需登录，直接填写信息</p>'
        + '<p class="notice">3、体检预约页面链接</p>'
        + '<p style="color: #1c84c6">http://117.157.64.139:9000/employee/reserve</p>'
        + '<p class="notice">备注：个人和企业（给员工预约）都在这里预约，如您遇到任何问题或困难，请及时与我们联系。我们将竭诚为您提供帮助，确保预约流程顺利进行。</p>'
        + '<p class="notice">三、给您带来的不便，我们深表歉意，也恳请各位市民予以谅解。我们将加快新系统的上线进度，尽快恢复正常的健康证办理工作，为大家提供更加便捷、高效的服务。</p>'
        + '<p class="notice">感谢您的理解与配合！</p>'
    ElMessageBox.alert(notice, title, {
            dangerouslyUseHTMLString: true,
            customStyle: {'max-width': '40%'}
        }
    )
}
